package com.bw.ssm.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

@Controller
public class AjaxController {

	@ResponseBody
	@RequestMapping("/page")
	public String page(HttpServletResponse response, Model model) throws IOException{
//		response.setContentType("text/html;charset=UTF-8");
//		PrintWriter out = response.getWriter();  
//        String data = "博客";    
//        out.println(data);
		return "list";
	}
	
	
	
	
	
	
	
	@RequestMapping("/list")
	public String list(HttpServletResponse response, Model model) throws IOException{
//		response.setContentType("text/html;charset=UTF-8");
//		PrintWriter out = response.getWriter();  
//        String data = "博客";    
//        out.println(data);
		return "list";
	}
	
	/**
	 * 直接返回字符串
	 * @param model
	 * @return 
	 */
	@ResponseBody
	@RequestMapping(value = "/ajax1",produces={"text/html;charset=UTF-8;","application/json;"})
//	@RequestMapping(value = "/ajax1",produces={"application/json;","text/html;charset=UTF-8;"}) 
//	@RequestMapping(value = "/ajax1",produces="text/html;charset=UTF-8;") 
//	@RequestMapping("/ajax1")
	public String ajax1(Model model){

		int r = 1 ;
		if(r==1){
//			return "true";
			return "添加成功！";
//			return  new String("添加成功！".getBytes(), "GBK");
//			return  new String("添加成功！".getBytes(), "ISO-8859-1");
//			return  new String("添加成功！".getBytes("ISO-8859-1"), "UTF-8");
		}else{
			return "添加失败！";
		}
	}
	/**
	 * 返回map
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/ajax2")
	public Map<String, String> ajax2(HttpServletResponse response,Model model){

		int r = 1 ;
		if(r==1){
			response.setHeader("Cache-Control", "max-age");
//			return "true";
			Map<String, String> map = new HashMap<String, String>();
			map.put("msg", "添加成功！");
			return map;
		}else{
			Map<String, String> map = new HashMap<String, String>();
			map.put("msg", "添加失败！");
			return map;
//			return "添加失败！";
		}
	}
	@ResponseBody
//	@RequestMapping(value = "/ajax12",produces={"text/html;charset=UTF-8;","application/json;"})
	@RequestMapping("/ajax12")
	public String ajax12(Model model){

		int r = 1 ;
		JSONObject jsonObject = new JSONObject();
		if(r==1){
			jsonObject.put("msg","添加成功！");
//			return "true";
			return jsonObject.toString();
//			return "添加成功！";
//			return  new String("添加成功！".getBytes(), "GBK");
//			return  new String("添加成功！".getBytes(), "ISO-8859-1");
//			return  new String("添加成功！".getBytes("ISO-8859-1"), "UTF-8");
		}else{
			jsonObject.put("msg","添加失败！");
			return "添加失败！";
		}
	}
	
}
